import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
import {MessageBox} from 'mint-ui';
import * as CODE_STATUS from '../utils/constantutils'
import * as types from './mutation-types'
import * as api from '../utils/api'
import * as http from '../utils/https'
import storageutils from '../utils/storageutils'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    token: null,//登录所需的token
    userProfile: {},//用户信息对象
    footerVisible: true,
    selectedTab: 'tool',
    pageOptions: {rows: 5, page: 1},

    medicalPurchasePendingDetail: {}, // 医耗待审批详情

    medicalPurchasePendingList: [], //医耗待审批列表
    medicalPurchasePendingObject: {total: 0, loading: false, page: 1},
    medicalPurchaseHistoryObject: {total: 0, loading: false, page: 1},
    medicalPurchaseHistoryList: [], //医耗已审批列表

    assetsPurchasePendingObject: {total: 0, loading: false, page: 1},//固定资产采购待审批列表页面对象
    assetsPurchaseHistoryObject: {total: 0, loading: false, page: 1},//固定资产采购已审批列表页面对象
    assetsWaitingApprovalList: [], //固定资产待审批
    assetsHisList: [],             //固定资产历史审批

    servicePurchasePendingObject: {total: 0, loading: false, page: 1},//后勤采购待审批列表页面对象
    servicePurchaseHistoryObject: {total: 0, loading: false, page: 1},//后勤采购已审批列表页面对象
    serviceWaitingApprovalList: [],  //后勤资产待审批
    serviceHisList: [],           //后勤资产历史审批

    assetsRecordPendingObject: {total: 0, loading: false, page: 1},//备案待审批列表页面对象
    assetsRecordHistoryObject: {total: 0, loading: false, page: 1},//备案采购已审批列表页面对象
    assetsRecordPendingList: [],           //备案待审批列表
    assetsRecordHisList: []                //备案已审批列表
  },
  getters: {
    getterPurchasePendingList(state) {
      return state.medicalPurchasePendingList;
    }
  },
  mutations: {
    [types.TOGGLE_FOOTER](state) {
      state.footerVisible = !state.footerVisible;
    },
    [types.SELECT_TAB](state, val) {
      state.selectedTab = val;
    },
    //登录操作
    [types.LOGIN](state, payload) {
      http.post(api.api_login, payload).then(response => {
        console.log(response);
        if (response.code == CODE_STATUS.CODE_200) //登录成功
        {
          let token = response.token;
          state.token = token;
          storageutils.setToken(token);
          state.userProfile = response.profile;//存下用户的登录信息
          //跳转到首页
          state.selectedTab = 'tool';
          router.push('/tool');
        } else {
          MessageBox('提示', '账号或者密码错误！');
        }
      });
    },
    //退出登录
    [types.LOGOUT](state, payload) {
      storageutils.clearToken();
      state.token = null;
      router.push('/');
    },

    //医耗采购单待审批列表
    [types.GET_PURCHASE_PENDING_LIST](state, payload) {

      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      http.fetch(api.api_medical_pending_purchase_list, params).then(response => {
        if (response.code == CODE_STATUS.CODE_200) {
          let total = response.total;
          let list = this.state.medicalPurchasePendingList.concat(response.data);
          this.state.medicalPurchasePendingList = list;
          state.medicalPurchasePendingObject.total = total;
          if (total == 0) { //空数据
            state.medicalPurchasePendingObject.loading = true;
          } else if (this.state.medicalPurchasePendingList.length >= total) { //全部加载完成
            state.medicalPurchasePendingObject.page = 1;
            state.medicalPurchasePendingObject.loading = true;
          } else {
            state.medicalPurchasePendingObject.page++;
            state.medicalPurchasePendingObject.loading = false;
          }

        }
        console.log(response);
      });
    },
    //医耗采购已审批列表接口
    [types.GET_MEDICAL_PURCHASE_HISTORY_LIST](state, payload) {
      console.log(payload);
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      console.log('医耗采购已审批列表接口 types.GET_MEDICAL_PURCHASE_HISTORY_LIST ');
      http.fetch(api.api_medical_history_purchase_list, params).then(response => {
        if (response.code == CODE_STATUS.CODE_200) {

          let total = response.total;
          state.medicalPurchaseHistoryObject.total = total;
          let list = this.state.medicalPurchaseHistoryList.concat(response.data);
          this.state.medicalPurchaseHistoryList = list;
          if (total == 0) { //空数据
            state.medicalPurchaseHistoryObject.loading = true;
          } else if (this.state.medicalPurchaseHistoryList.length >= total) { //全部加载完成
            state.medicalPurchaseHistoryObject.page = 1;
            state.medicalPurchaseHistoryObject.loading = true;
          } else {
            state.medicalPurchaseHistoryObject.page++;
            state.medicalPurchaseHistoryObject.loading = false;
          }

        }
        console.log(response);
      });
    },
    //采购单详情接口
    [types.GET_PURCHASE_PENDING_DETAIL](state, payload) {
      let purchaseId = payload.purchaseId;
      let taskID = payload.taskID;
      http.fetch(api.api_medical_pending_purchase_detail, {purchaseId: purchaseId}).then(response => {
        console.log(response);
        if (response.code == CODE_STATUS.CODE_200) {
          state.medicalPurchasePendingDetail = response;
        }
      });

    },
    [types.AUDIT_PURCHASE_TASK](state, payload) {
      console.log(pay);
    },
    //固定资产采购单待审批列表
    [types.GET_ASSETS_WAITING_APPROVAL_LIST](state, payload) {
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //待审批
      http.fetch(api.api_assets_waiting_approval_list, params).then(response => {
        console.log(response);
        if (response.code == 200) {
          let total = response.total;
          let list = state.assetsWaitingApprovalList.concat(response.data);
          state.assetsWaitingApprovalList = list;
          state.assetsPurchasePendingObject.total = total;
          if (total == 0) { //空数据
            state.assetsPurchasePendingObject.loading = true;
          } else if (state.assetsWaitingApprovalList.length >= total) { //全部加载完成
            state.assetsPurchasePendingObject.page = 1;
            state.assetsPurchasePendingObject.loading = true;
          } else {
            state.assetsPurchasePendingObject.page++;
            state.assetsPurchasePendingObject.loading = false;
          }

        }
        console.log(response);
      });

    },
    //固定资产已审批列表接口
    [types.GET_ASSETS_HISTORY_APPROVAL_LIST](state,payload){
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //已审批
      http.fetch(api.api_assets_his_list, params).then(response => {
        if (response.code = 200) {
          // this.state.serviceHisList = response.data;
          let total = response.total;
          state.assetsPurchaseHistoryObject.total = total;
          let list = state.assetsHisList.concat(response.data);
         state.assetsHisList = list;
          if (total == 0) { //空数据
            state.assetsPurchaseHistoryObject.loading = true;
          } else if (this.state.assetsHisList.length >= total) { //全部加载完成
            state.assetsPurchaseHistoryObject.page = 1;
            state.assetsPurchaseHistoryObject.loading = true;
          } else {
            state.assetsPurchaseHistoryObject.page++;
            state.assetsPurchaseHistoryObject.loading = false;
          }
        }
      });
    },


    //后勤资产采购单待审批列表
    [types.GET_SERVICE_WAITING_APPROVAL_LIST](state, payload) {
      // let params = this.state.pageOptions;
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //待审批
      http.fetch(api.api_service_waiting_approval_list, params).then(response => {
        if (response.code == 200) {

          let total = response.total;
          let list = this.state.serviceWaitingApprovalList.concat(response.data);
          this.state.serviceWaitingApprovalList = list;
          state.servicePurchasePendingObject.total = total;
          if (total == 0) { //空数据
            state.servicePurchasePendingObject.loading = true;
          } else if (this.state.serviceWaitingApprovalList.length >= total) { //全部加载完成
            state.servicePurchasePendingObject.page = 1;
            state.servicePurchasePendingObject.loading = true;
          } else {
            state.servicePurchasePendingObject.page++;
            state.servicePurchasePendingObject.loading = false;
          }

        }
        console.log(response);
      });

    },
    // 后勤采购单已审批列表
    [types.GET_SERVICE_HISTORY_APPROVAL_LIST](state, payload) {
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //已审批
      http.fetch(api.api_service_his_list, params).then(response => {
        if (response.code = 200) {
          // this.state.serviceHisList = response.data;
          let total = response.total;
          state.servicePurchaseHistoryObject.total = total;
          let list = this.state.serviceHisList.concat(response.data);
          this.state.serviceHisList = list;
          if (total == 0) { //空数据
            state.servicePurchaseHistoryObject.loading = true;
          } else if (this.state.serviceHisList.length >= total) { //全部加载完成
            state.servicePurchaseHistoryObject.page = 1;
            state.servicePurchaseHistoryObject.loading = true;
          } else {
            state.servicePurchaseHistoryObject.page++;
            state.servicePurchaseHistoryObject.loading = false;
          }
        }
      });
    },

    //固定资产备案待审批
    [types.GET_ASSETS_RECORD_PENDING_LIST](state, payload) {

      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //待审批
      http.fetch(api.api_assets_record_pending_list, params).then(response => {
        if (response.code == 200) {

          let total = response.total;
          let list = this.state.assetsRecordPendingList.concat(response.data);
          this.state.assetsRecordPendingList = list;
          state.assetsRecordPendingObject.total = total;
          if (total == 0) { //空数据
            state.assetsRecordPendingObject.loading = true;
          } else if (this.state.assetsRecordPendingList.length >= total) { //全部加载完成
            state.assetsRecordPendingObject.page = 1;
            state.assetsRecordPendingObject.loading = true;
          } else {
            state.assetsRecordPendingObject.page++;
            state.assetsRecordPendingObject.loading = false;
          }

        }
        console.log(response);
      });

    },
    //固定资产备案单已审批列表接口
    [types.GET_ASSETS_RECORD_HISTORY_LIST](state,payload){
      let params = Object.assign(payload, {rows: state.pageOptions.rows});
      //已审批
      http.fetch(api.api_assets_record_his_list, params).then(response => {
        if (response.code = 200) {
          // this.state.serviceHisList = response.data;
          let total = response.total;
          state.assetsRecordHistoryObject.total = total;
          let list = this.state.assetsRecordHisList.concat(response.data);
          this.state.assetsRecordHisList = list;
          if (total == 0) { //空数据
            state.assetsRecordHistoryObject.loading = true;
          } else if (this.state.assetsRecordHisList.length >= total) { //全部加载完成
            state.assetsRecordHistoryObject.page = 1;
            state.assetsRecordHistoryObject.loading = true;
          } else {
            state.assetsRecordHistoryObject.page++;
            state.assetsRecordHistoryObject.loading = false;
          }
        }
      });
    },


    [types.RESET_MEIDCAL_PURCHASE_LIST_PAGE](state) {

      state.medicalPurchasePendingObject = {total: 0, loading: false, page: 1};
      state.medicalPurchasePendingList = []
      state.medicalPurchaseHistoryObject = {total: 0, loading: false, page: 1};
      state.medicalPurchaseHistoryList = []
    },
    [types.RESET_SERVICE_PURCHASE_LIST_PAGE](state) {
      state.servicePurchasePendingObject = {total: 0, loading: false, page: 1};
      state.serviceWaitingApprovalList = []
      state.servicePurchaseHistoryObject = {total: 0, loading: false, page: 1};
      state.serviceHisList = []
    },
    [types.RESET_ASSETS_PURCHASE_LIST_PAGE](state) {
      state.assetsPurchasePendingObject = {total: 0, loading: false, page: 1};
      state.assetsWaitingApprovalList = []
      state.assetsPurchaseHistoryObject = {total: 0, loading: false, page: 1};
      state.assetsHisList = []
    },
    [types.RESET_ASSETS_RECORD_LIST_PAGE](state){
      state.assetsRecordPendingObject = {total: 0, loading: false, page: 1};
      state.assetsRecordPendingList = []
      state.assetsRecordHistoryObject = {total: 0, loading: false, page: 1};
      state.assetsRecordHisList = []
    }
  },
  actions: {
    //登录
    login({commit}, payload) {
      console.log('login actions');
      commit(types.LOGIN, payload);
    },
    //退出登录
    logout({commit}, payload) {
      console.log('logout actions');
      commit(types.LOGOUT);
    },
    //医耗采购单待审批列表
    getPurchasePendingList({commit}, payload) {
      console.log('getPurchasePendingList actions');
      commit(types.GET_PURCHASE_PENDING_LIST, payload);
    },
    //医耗采购单待审批详情数据
    getMedicalPurchaseDetail({commit}, payload) {
      commit(types.GET_PURCHASE_PENDING_DETAIL, payload);
    },
    //医耗审批接口
    auditPurchaseTask({commit}, payload) {
      console.log('auditPurchaseTask actions');
      http.post(api.api_audit_purchase, payload).then(response => {
        console.log(response)
        if (response.code == CODE_STATUS.CODE_200) {
          commit(types.GET_PURCHASE_PENDING_LIST);
        }
        MessageBox('提示', response.msg);
      })
    },
    //固定资产采购单待审批列表
    getAssetsWaitingApprovalList({commit}, payload) {
      console.log('getAssetsWaitingApprovalList actions');
      commit(types.GET_ASSETS_WAITING_APPROVAL_LIST,payload);
    },
    //固定资产采购单已审批列表
    getAssetsHistoryApprovalList({commit}, payload) {
      console.log('getAssetsWaitingApprovalList actions');
      commit(types.GET_ASSETS_HISTORY_APPROVAL_LIST,payload);
    },
    //后勤资产采购单待审批列表
    getServiceWaitingApprovalList({commit}, payload) {
      console.log('getServiceWaitingApprovalList actions');
      commit(types.GET_SERVICE_WAITING_APPROVAL_LIST, payload);
    },
    //后勤资产采购单已审批列表
    getServiceHistoryApprovalList({commit}, payload) {
      commit(types.GET_SERVICE_HISTORY_APPROVAL_LIST, payload);
    },
    //后勤审批接口
    auditServicePurchaseTask({commit}, payload) {
      console.log('auditServicePurchaseTask actions');
      http.post(api.api_audit_purchase, payload).then(response => {
        console.log(response)
        if (response.code == CODE_STATUS.CODE_200) {
          commit(types.GET_SERVICE_WAITING_APPROVAL_LIST);
        }
        MessageBox('提示', response.msg);
      })
    },
    //固定资产审批接口
    auditAssetsPurchaseTask({commit}, payload) {
      console.log('auditAssetsPurchaseTask actions');
      http.post(api.api_audit_purchase, payload).then(response => {
        console.log(response)
        if (response.code == CODE_STATUS.CODE_200) {
          commit(types.GET_ASSETS_WAITING_APPROVAL_LIST);
        }
        MessageBox('提示', response.msg);
      })
    },
    //备案单待审批列表接口
    getAssetsRecordPendingList({commit},payload) {
      console.log('getAssetsRecordPendingList actions');
      commit(types.GET_ASSETS_RECORD_PENDING_LIST,payload);
    },
    //备案单已审批列表接口
    getAssetsRecordHistoryList({commit},payload) {
      console.log('getAssetsRecordPendingList actions');
      commit(types.GET_ASSETS_RECORD_HISTORY_LIST,payload);
    },
    //固定资产备案审批接口
    auditAssetsRecordTask({commit}, payload) {
      console.log('auditAssetsRecordTask actions');
      http.post(api.api_audit_assets_record, payload).then(response => {
        console.log(response)
        if (response.code == CODE_STATUS.CODE_200) {
          commit(types.GET_ASSETS_RECORD_PENDING_LIST);
        }
        MessageBox('提示', response.msg);
      })
    },
    //医耗采购单已审批列表接口
    getMedicalPurchaseHistoryList({commit}, payload) {
      console.log("医耗采购已审批列表分页数据 getMedicalPurchaseHistoryList ");
      commit(types.GET_MEDICAL_PURCHASE_HISTORY_LIST, payload);
    },
    //医耗采购列表页 页面数据初始化
    resetMedicalPurchaseListPage({commit}) {
      console.log('医耗采购单列表页面数据初始化 types.RESET_MEIDCAL_PURCHASE_LIST_PAGE');
      commit(types.RESET_MEIDCAL_PURCHASE_LIST_PAGE);
    },
    //后勤采购单列表 页面数据初始化
    resetServicePurchaseListPage({commit}) {
      commit(types.RESET_SERVICE_PURCHASE_LIST_PAGE);
    },
    //固定资产采购单列表 页面数据初始化
    resetAssetsPurchaseListPage({commit}) {
      commit(types.RESET_ASSETS_PURCHASE_LIST_PAGE);
    },
    //固定资产备案单列表 页面数据初始化
    resetAssetsRecordListPage({commit}) {
      commit(types.RESET_ASSETS_RECORD_LIST_PAGE);
    }
  }
});
export default store;
